﻿
using System;
using System.Collections.Generic;
using System.Text;

using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model;

namespace YouHP.Pages.WeightHistory
{
    public class HistoryAdd : YouHP.Pages.PatientBasePage
    {
        protected WeightHistoryInfo WeightData { get; set; }
        protected double Height { get; set; }

        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            HandleCommon();
            if (ErrorCount > 0) return;

            if (IsPost) HandlePost();
            else HandleGet();
        }

        protected new void HandleCommon()
        {
            this.AddLocation("体重记录", "/WeightHistory/Index.aspx");
            this.AddLocation("添加体重记录", "/WeightHistory/HistoryAdd.aspx");
            this.SetTitle("添加体重记录");

            //取得身高
            Height = PatientBLL.Instance.GetPatientHeight(UserData.Id);
        }

        protected new void HandlePost()
        {
            WeightHistoryInfo info = ValidateForm();
            if (info == null) return;
            bool succ = true;         

            succ = PatientBLL.Instance.AddWeightHistory(info);
            if (succ == true)
            {
               

                this.AddMsgLine("添加成功");
                this.Redirect("/WeightHistory/Index.aspx");
            }
            else
            {
                this.AddErrLine("添加失败");
            }
        }

        protected WeightHistoryInfo ValidateForm()
        {
            WeightHistoryInfo info = new WeightHistoryInfo();

            double weight = RequestTools.GetFormDouble("wvalue", -1);
            if (weight <= 0)
            {
                this.AddErrLine("请填写正确的体重信息");
                return null;
            }
            info.DataValue = weight;

            // 对时间进行判断
            string va = RequestTools.GetFormString("odate");
            if (va == "")
            {
                this.AddErrLine("请填写时间");
                return null;
            }

            DateTime dt;
            bool succ = DateTime.TryParse(va, out dt);
            if (succ == false)
            {
                this.AddErrLine("错误的时间 " + va);
                return null;
            }

            if (DateTime.Compare(dt, DateTime.Today) > 0)
            {
                this.AddErrLine("记录日期 " + dt.ToString("yyyy-MM-dd") + " 不能比当前日期 " + DateTime.Today.ToString("yyyy-MM-dd") + " 晚！");
                return null;
            }
            else if (DateTime.Compare(dt, PrData.StartDate) < 0)
            {
                this.AddErrLine("记录日期 " + dt.ToString("yyyy-MM-dd") + " 不能比第一次发病日期 " + PrData.StartDate.ToString("yyyy-MM-dd") + " 早！");
                return null;
            }

            // 检查时间是否重复
            if (PatientBLL.Instance.CheckWeightHistory(PrData.Id, dt))
            {
                //提示是否需要覆盖历史记录
                this.AddErrLine("是否需要覆盖" + va + "已有的历史记录？");
                return null;
            }
            info.RecordDate = dt;

            info.PRId = PrData.Id;
            info.UpdateDate = DateTime.Now;

            return info;
        }

        protected new void HandleGet()
        {
            this.AddDatePicker();
            this.AddCommonJS();

            WeightData = new WeightHistoryInfo();
        }
    }
}